package com.yami.shop.app.utils;

import java.io.Serializable;

public class HttpResult<T> implements Serializable {
    /**
     * 是否相应成功
     * */
    private Boolean success;
    /**
     * 响应状态码
     * */
    private Integer resultCode;
    /**
     * 响应数据
     * */
    private T data;
    /**
     * 错误信息
     * */
    private String resultMsg;
    //构造方法
    /**
     * 无参构造器（构造器私有，外部不可直接创建）
     * */
    private  HttpResult(){
        this.resultCode = 200;
        this.success = true;
    }
    /**
     * 有参构造器
     * @param obj
     * */
    private HttpResult(T obj){
        this.resultCode = 200;
        this.data = obj;
        this.success = true;
    }

    /**
     * 有参构造器
     * @param resultCode
     * */
    private HttpResult(ResultCodeEnum resultCode){
        this.success = false;
        this.resultCode = resultCode.getResultCode();
        this.resultMsg = resultCode.getResultMsg();
    }
    private HttpResult(String message){
        this.resultCode = 500;
        this.success = false;
        this.resultMsg = message;
    }

    /**
     * 通用发挥成功（没有返回结果）
     * @return
     * */
    public static<T> HttpResult<T> success(){
        return new HttpResult();
    }
    /**
     * 返回成功（有返回结果）
     * @param  data
     * @param <T>
     * @return
     * */
    public static<T> HttpResult<T> success(T data){
        return new HttpResult<T>(data);
    }

    /**
     * 通用返回失败(有返回结果)
     * @param resultCodeEnum
     * @param <T>
     * @return
     * */
    public static<T> HttpResult<T> failure(ResultCodeEnum resultCodeEnum){
        return new HttpResult<>(resultCodeEnum);
    }
    /**
     * 通用返回失败（无返回结果）
     * @return
     * */
    public static<T> HttpResult<T> failure(String message){
        return new HttpResult<>(message);
    }


    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Integer getResultCode() {
        return resultCode;
    }

    public void setResultCode(Integer resultCode) {
        this.resultCode = resultCode;
    }

    public String getResultMsg() {
        return resultMsg;
    }

    public void setResultMsg(String resultMsg) {
        this.resultMsg = resultMsg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }




}
